<?php
namespace App\Http\Controllers\Api\Trade;

use Illuminate\Http\Request;
use App\Http\Controllers\Api\Controller;
use App\Http\Requests\Trade\Shop\SupplyListRequest;
use App\Http\Requests\Trade\Shop\DemandListRequest;
use App\Http\Resources\Trade\Shop\DetailResource as ShopDetailResource;
use App\Http\Resources\Trade\Supply\ListResource as SupplyListResource;
use App\Http\Resources\Trade\Demand\ListResource as DemandListResource;
use App\Models\Trade\Shop\ShopCollectionModel;
use App\Services\Trade\ShopService;
use App\Services\Trade\SupplyService;
use App\Services\Trade\DemandService;

class ShopController extends Controller
{

    /**
     * 店铺详情
     *
     * @param $id 必选            
     */
    public function getDetail(Request $request)
    {
        $this->validate($request, [
            'id' => 'required|integer|min:1'
        ]);
        $id = $request->query('id');

        $shopService = new ShopService();
        $data = $shopService->findDetail($id)->shift();
        $data = ShopDetailResource::makeDataDecode($data);
        
        // 关注判断
        $data['collection'] = $this->identity()->isValid() && ShopCollectionModel::findItem($this->identity()->getUserId(), $id)->count() ? 1 : 0;
        
        return $this->response->setResponseData($data)->getResponse();
    }

    /**
     * 出售列表
     *
     * @param $page 可选            
     * @param $page_size 可选            
     * @param $id 必选            
     */
    public function getSupplyList(SupplyListRequest $request)
    {
        $page = intval($request->query('page', 1));
        $pageSize = intval($request->query('page_size', 50));
        $param['shop_id'] = $request->query('id');
        
        $supplyService = new SupplyService();
        list ($collection, $count) = $supplyService->selectList($page, $pageSize, $param);
        
        $data['count'] = [
            'total' => $count,
            'page' => $page,
            'page_size' => $pageSize
        ];
        $data['data'] = new SupplyListResource($collection);
        $data['uri'] = ShopDetailResource::getListUri($param['shop_id']);
        
        return $this->response->setResponseData($data)->getResponse();
    }

    /**
     * 出售列表
     *
     * @param $page 可选            
     * @param $page_size 可选            
     * @param $id 必选            
     */
    public function getDemandList(DemandListRequest $request)
    {
        $page = intval($request->query('page', 1));
        $pageSize = intval($request->query('page_size', 50));
        $param['shop_id'] = $request->query('id');
        
        $demandService = new DemandService();
        list ($collection, $count) = $demandService->selectList(1, $pageSize, $param);
        
        $data['count'] = [
            'total' => $count,
            'page' => $page,
            'page_size' => $pageSize
        ];
        $data['data'] = new DemandListResource($collection);
        $data['uri'] = ShopDetailResource::getListUri($param['shop_id']);
        
        return $this->response->setResponseData($data)->getResponse();
    }
}
